package com.djq.hhc.controller;

import com.djq.hhc.holder.UserHolder;
import com.djq.hhc.model.credit.CreditExchange;
import com.djq.hhc.model.shop.ShopOrder;
import com.djq.hhc.result.Result;
import com.djq.hhc.service.CreditExchangeService;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: dadd7
 * @Date: 2023/03/05/下午 5:01
 * @Description:
 */

@RestController
@RequestMapping(value = "/credit/creditExchange")
public class CreditExchangeController {

    @Autowired
    private CreditExchangeService creditExchangeService;

    @PutMapping("exchange/{orderId}/{credit}/{price}")
    public boolean exchange(@RequestHeader("Authorization") String token,
                            @PathVariable String orderId,
                            @PathVariable Integer credit,
                            @PathVariable Integer price) {
        Long userId = UserHolder.getUser().getId();
        if (userId == null) {
            return false;
        }
        CreditExchange creditExchange = new CreditExchange();
        creditExchange.setUserId(userId);
        creditExchange.setOrderId(Long.valueOf(orderId));
        creditExchange.setPreCredit(credit);
        creditExchange.setNowCredit(credit - price);
        boolean save = creditExchangeService.save(creditExchange);
        return save;
    }

//    @GetMapping("createCredit")
//    public boolean createCredit(@RequestBody ShopOrder shopOrder) {
//        return creditExchangeService.createCredit(shopOrder);
//    }
}
